#include "DateTime.h"

namespace SUNSHINE
{
	time_t DateTime::timer;
	struct tm * DateTime::lt;

	int DateTime::Millisecond()
	{
		return DateTime::Microsecond() / 1000;
	}

	int DateTime::DayOfWeek()
	{
		DateTime::Refresh();
		return DateTime::lt->tm_wday;
	}

	int DateTime::DayOfYear()
	{
		DateTime::Refresh();
		return DateTime::lt->tm_yday;
	}

	string DateTime::Time(char delimiter /* = '\0' */)
	{
		char result[6 + 2 + 1]; // sizeof(hour + minute + second + ${delimiter} * 2 + '\0')

		string format("%02d");

		DateTime::CreateFormatStringBy(format, delimiter);

		DateTime::Refresh();
		snprintf(result, sizeof(result),
					format.c_str(),
					DateTime::Hour(),
					DateTime::Minute(),
					DateTime::Second());

		return result;
	}

	string DateTime::Date(char delimiter /* = '\0' */)
	{
		char result[8 + 2 + 1]; // sizeof(year + month + day + ${delimiter} * 2 + '\0')

		string format("%04d");

		DateTime::CreateFormatStringBy(format, delimiter);

		DateTime::Refresh();

		snprintf(result, sizeof(result),
					format.c_str(),
					DateTime::Year(),
					DateTime::Month(),
					DateTime::Day());

		return result;
	}

	string DateTime::DateAndTime(char dateDelimiter /* = '\0' */,
				char timeDelimiter /* = '\0' */,
				const string & delimiterBetweenDateAndTime /* = "" */)
	{
		string result;
		result.append(DateTime::Date(dateDelimiter))
			.append(delimiterBetweenDateAndTime)
			.append(DateTime::Time(timeDelimiter));

		return result;
	}
}




